
module.exports = function () {

    this.store = {
        __work_path__ : 1,
        __bh_version__: 7000,
        __script_path__: "",
        __project_json__ : JSON.parse(files.read("project.json")),
        __const__: {
            HEIGHT: device.height,
            WIDTH: device.width,
            ANIMATION_DELAY: 250,
            SCRIPT_ENGINE_NAME: "bh_script"
        },
        __obj__: {
            orerlay_window: null,
            config_window: null,
            user_window: null
        },
        //存储用户Id
        __ids__:[],
        paojiaoyun: {
            user: "",
            key: "",
            secret: "",
            status: {
                
            }
        },
        __status__ : {
            window_status: false,
            script_status: false,
            log_status: false,
            setting_status: false,
            user_status: false,
            orerlay_status: false,
            init: false,
            run: false,
            login: false,
            drawUI: false,
        },
        
        __need_switch__ : {
            //启动
            runCheked: 1,
            //日志
            logChecked: 1,
            //表单
            fromChecked: 0,
            //我的
            userChecked: 0,
            //退出
            quitChecked: 1,
        },
        __need_server__ : {
            //无障碍
            autoServer: 0,
            //悬浮窗
            floatyServer: 1,
            //调试
            deBug: 1
        },
        // 个人中心列表
        __user_list__: [
            [
                {
                    title: "账号：",
                    icon: "kehuquanyi",
                    value: "",
                    visibility: "visible"
                },
                {
                    title: "状态：",
                    icon: "vip",
                    value: "",
                    visibility: "visible"

                },
                {
                    title: "卡密：",
                    icon: "creditcard",
                    value: "",
                    visibility: "visible"

                },
                {
                    title: "到期时间：",
                    icon: "shijian",
                    value: "",
                    visibility: "visible"

                }
            ],
            [
                {
                    title: "安卓版本：",
                    icon: "robot",
                    value: "",
                    visibility: "visible"

                },
                {
                    title: "设备型号：",
                    icon: "gengduo",
                    value: "",
                    visibility: "visible"
                },
                {
                    title: "IMEI：",
                    icon: "electronics",
                    value: "",
                    visibility: "visible"
                },
            ],

        ],
        
    }

    this.storage = storages.create(this.store.__project_json__.packageName)
    
    let t = this.store
    this.mutations = {

        //保存view内容到本地
        save_view : function () {
            if (this.store.__ids__.length == 0) return;
            
            this.store.__ids__.forEach(function(v, i) {
                if (v == null || v == "") return;
                //获取控件类型
                let view = this.store.__obj__.config_window[v]

                let className = view.getClass().toString().split(" ")[1]
                let value = null;
                switch (className) {
                    case "com.stardust.autojs.core.ui.widget.JsEditText":
                        console.log("ajedit")
                        //取值、写入
                        value = view.getText()
                        console.log(value)
                        this.storage.put(v, {
                            type: "edit",
                            value: value
                        })
                    break;
                    case "android.widget.EditText":
                        console.log("edit")
                        value = view.getText()
                        console.log(value)
                        this.storage.put(v, {
                            type: "edit",
                            value: value
                        })
                    break;
                    case "android.widget.CheckBox":
                        console.log("多选")
                        value = view.isChecked()
                        console.log(value)
                        this.storage.put(v, {
                            type: "checked",
                            value: value
                        })
                    break;
                    case "android.widget.RadioButton":
                        console.log("单选")
                        value = view.isChecked()
                        console.log(value)
                        this.storage.put(v, {
                            type: "radio",
                            value: value
                        })
                    break;
                    case "com.stardust.autojs.core.ui.widget.JsSpinner":
                        console.log("下拉菜单")
                        value = view.getSelectedItemPosition()
                        console.log(value)
                        this.storage.put(v, {
                            type: "spinner",
                            value: value
                        })
                    break;
                    case "android.widget.SeekBar":
                        console.log("拖动条")
                        value = view.getProgress()
                        console.log(value)
                        this.storage.put(v, {
                            type: "seekbar",
                            value: value
                        })
                    break;
                    default:
                        console.log(className)
                        console.log("不明类型")

                }
                


            }.bind(this))
        }.bind(this),

        //读取本地view数据写入ui
        completion_view : function () {
            if (this.store.__ids__.length == 0) return;
            this.store.__ids__.forEach(function(v, i) {

                let v_class_name = this.store.__obj__.config_window[v].getClass().toString().split(" ")[1]
                if (v_class_name == "com.stardust.autojs.core.ui.widget.JsEditText" || v_class_name == "android.widget.EditText") {
                    
                    //添加焦点
                    this.store.__obj__.config_window[v].on("touch_down", function() {
                        this.store.__obj__.config_window.requestFocus()
                        this.store.__obj__.config_window[v].requestFocus()
                    }.bind(this))
                }


                //读取本地数据
                console.log("读取"+v+"的本地数据")
                let view_data = this.storage.get(v)
                if (view_data == null || view_data == "") return;
                console.log("开始载入UI配置")


                
                switch(view_data.type){
                    case "edit":
                        this.store.__obj__.config_window[v].setText(view_data.value)
                    break;
                    case "checkbox":
                        this.store.__obj__.config_window[v].setChecked(view_data.value)
                    break;
                    case "radio":
                        this.store.__obj__.config_window[v].setChecked(view_data.value)
                    break;
                    case "spinner":
                        this.store.__obj__.config_window[v].setSelection(view_data.value)
                    break;
                    case "seekbar":
                        this.store.__obj__.config_window[v].setProgress(view_data.value)
                    break;
                }
                
            }.bind(this))
        }.bind(this),

        //初始化遮罩
        init_orerlay : function () {
            if (t.__status__.orerlay_status && t.__obj__.orerlay_window != null) {
                t.__obj__.orerlay_window.close()
            }
            t.__obj__.orerlay_window = floaty.rawWindow(
                <frame bg="#66000000"></frame>
            )
            t.__obj__.orerlay_window.setSize(0, 0)
            t.__obj__.orerlay_window.setTouchable(false)
        },

        //打开遮罩层
        open_orerlay: function () {
            if (!t.__status__.orerlay_status) {
                console.log("打开遮罩层")

                t.__obj__.orerlay_window.setSize(-1, -1)
                t.__status__.orerlay_status = !t.__status__.orerlay_status
            }
            
        },

        close_orerlay: function () {
            if (t.__status__.orerlay_status) {
                console.log("关闭遮罩层")
                t.__obj__.orerlay_window.setSize(0, 0)
                t.__status__.orerlay_status = !t.__status__.orerlay_status
            }

        },

        getImageBase : function (imgName, suffix) {
            if (suffix == null) suffix = ".png";
            let filePath = "mod/img/"+imgName+suffix
            if (!files.exists(filePath)) {
                console.log(filePath+"文件不存在")
                return null
            }
            
            let res = images.read(filePath)
            let base = "data:image/png;base64,"+images.toBase64(res, "png", 100)
            res.recycle()
            return base
        },

        //封装判断脚本引擎是否存活方法
        isEngineAlive : function () {
            for (key in engines.all()) {
                if (engines.all()[key].source == t.__const__.SCRIPT_ENGINE_NAME+".js") {
                    return true
                }
            }
            return false
        },

        //获取图标显示状态，默认隐藏
        to_icon_visibility : function (stat) {
            if (stat == 0) {
                return "gone"
            } else if(stat == 1) {
                return "visible"
            } else {
                return "gone"
            }
        },

        //启动脚本方法
        run_script : function (script_file_name) {
            
            engines.execScript(t.__const__.SCRIPT_ENGINE_NAME, files.read(script_file_name))
        },

        //封装脚本停止方法
        stop_script : function () {
            for (key in engines.all()) {
                if (engines.all()[key].source == t.__const__.SCRIPT_ENGINE_NAME+".js") {
                    engines.all()[key].forceStop()
                }
            }
            return null
        },

        //堵塞获取服务
        wait_server: function () {
            console.log(t.__project_json__)
            for (key in t.__need_server__) {
                console.log(key)
                if (t.__need_server__[key] == 0) continue;
                switch (key) {
                    case "autoServer":
                        if (auto.service == null) {
                            let str = "请打开【" + t.__project_json__.name + "】无障碍服务！"
                            toastLog(str);
                            auto.waitFor()
                        }
                        console.log("【无障碍服务】已打开！");
                        break;
                    case "floatyServer":
                        if (!floaty.checkPermission()) {
                            let str = "请打开【" + t.__project_json__.name + "】悬浮窗服务！"
                            toastLog(str);
                            floaty.requestPermission()
                            while(!floaty.checkPermission());
                            console.log("【悬浮窗权限】已打开");
                        } else {
                            console.log("已获取悬浮窗权限！")
                        }
                        break;
                }
            }
            return 0
        },

        //泡脚云登录
        pjysdk_login: function () {
            let login_ret = this.pjysdk.CardLogin()
            console.log(login_ret)
            toastLog(login_ret.message)
            if (login_ret.code == 0) {
                this.storage.put("pjysdk_kami", this.pjysdk._card)
                this.storage.put("pjysdk_login_ret", login_ret)
                this.mutations.pjysdk_updata(login_ret.result)
            } else if (login_ret.code == 10210) {
                this.storage.put("pjysdk_kami", this.pjysdk._card)
                this.mutations.pjysdk_updata({
                    card_type: login_ret.message,
                    expires: "null"
                })
            } else if (login_ret.code == 10213) {
                //超出多开上限
                
            }
        }.bind(this),
        //泡脚云充值
        pjysdk_recharge : function (kami) {
            

        },

        //更新用户数据
        pjysdk_updata: function (upv) {
            this.store.__user_list__[0].map(function(v, i) {
                console.log(v.title)

                switch (v.title) {
                    case "账号：":
                        v.value = this.pjysdk.getDeviceID()
                        
                    break;
                    case "状态：":
                        v.value = upv.card_type

                    break;
                    case "卡密：":
                        v.value = this.pjysdk._card || "null"

                    break;
                    case "到期时间：":
                        v.value = upv.expires
                    break
                }
                return v
            }.bind(this))

            ui.run(function() {
                this.store.__obj__.user_window.user_list_view.setVisibility(android.view.View.VISIBLE)

                this.store.__obj__.user_window.user_list_view.setDataSource(this.store.__user_list__[0])
                
            }.bind(this))
            
        }.bind(this)
    }
}

